package servletAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;

import util.DomainUtils;
import util.FormArgumentsException;
import actionForm.ActionGroupDispatcherValidationForm;
import domain.dao.interfaces.IDAOContact;
import domain.dao.interfaces.IDAOContactGroup;
import domain.entities.interfaces.IContactGroup;

public class ActionGroupDispatcherAction extends Action {

	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse) {

		final ActionGroupDispatcherValidationForm lForm = (ActionGroupDispatcherValidationForm) pForm;

		String current_groups_versions = lForm.getCurrent_groups_versions();
		String selected_contactgroup = lForm.getSelected_contactgroup();
		String wanted_action = lForm.getWanted_action();
		String new_group_name = lForm.getNew_group_name();

		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {

			if(wanted_action.compareTo("show_details_contactgroup") == 0) {
				pRequest.getRequestDispatcher("pages/showContactGroupDetails.jsp").forward(pRequest, pResponse);
			} 
			else if(wanted_action.compareTo("modify_contactgroup") == 0) {

				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				int groupId = Integer.parseInt(selected_contactgroup);
				boolean found = false;

				for(String cgVersion : current_groups_versions.split("/")) {

					if(cgVersion.isEmpty() == false) {
						String[] cgVersionSplited = cgVersion.split(";");
						int groupIdTmp = Integer.parseInt(cgVersionSplited[0]);
						int groupVersionTmp = Integer.parseInt(cgVersionSplited[1]);

						if(groupId == groupIdTmp) {
							IContactGroup cg = daoContactGroup.getById(groupId);

							checks(pRequest, cg, groupVersionTmp);

							daoContactGroup.updateContactGroup(
									cg,
									Integer.parseInt(selected_contactgroup), 
									new_group_name);

							DomainUtils.displayInfoMessage(
									pRequest, 
									DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
									"The contact group name has been successfully updated.");
							found = true;
							break;
						}
					}
				}

				if(found == false) {
					throw new Exception();
				}

				return pMapping.findForward("stay");
			} 
			else if(wanted_action.compareTo("delete_contactgroup") == 0) {
				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				daoContactGroup.deleteContactGroup(
						Integer.parseInt(selected_contactgroup));

				DomainUtils.displayInfoMessage(
						pRequest, 
						DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
						"The contact group has been successfully deleted.");


				return pMapping.findForward("stay");
			} 
			else if(wanted_action.compareTo("add_contact") == 0) {

				return pMapping.findForward("add");
			} 
			else if(wanted_action.compareTo("remove_contact") == 0) {

				return pMapping.findForward("remove");
			} 

			//there is nothing to do
			return pMapping.findForward("stay");
		}
		catch(Exception e) {

			try {
				IDAOContactGroup daoContactGroup = 
						(IDAOContactGroup) context.getBean("beanDAOContactGroup");

				if(e instanceof HibernateOptimisticLockingFailureException) {
					pRequest.setAttribute(
							DomainUtils.ALERT_ERROR_STRING, 
							"This contact group has just been modified. Please, before submit check related new informations.");
				}
				else if(daoContactGroup.getById(Integer.parseInt(selected_contactgroup)) == null) {
					pRequest.setAttribute(
							DomainUtils.ALERT_ERROR_STRING, 
							"This contact group has just been deleted.");
				}
				else if((e instanceof FormArgumentsException) == false) {
					pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server."+e);
				}
			}
			catch(Exception e2) {
				pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"An error occur on the server.");
			}

			DomainUtils.keepParametersContent(pRequest);

			return pMapping.findForward("stay");
		}
	}

	private void checks(HttpServletRequest request, IContactGroup cg,
			int groupVersionTmp) throws FormArgumentsException {

		if(cg.getVersion() != groupVersionTmp) {

			request.setAttribute(
					DomainUtils.ALERT_ERROR_STRING, 
					"This contact group has just been modified. Please, before submit check related new informations.");

			throw new FormArgumentsException();
		}
	}
}
